<?php
// +-------------------------------------------------------------------
// |
// +-------------------------------------------------------------------
// | Copyright (c) 2009-2016 All rights reserved.
// +-------------------------------------------------------------------
namespace Service\Api\V1;

use Think\Model;

class HouseContractList
{
    public $login = false; //是否需要登录才能访问该接口
    public $title = "租赁合同列表ok";
    public $group = '用户';
    public $desc = "";

    public $input = [
    ];

    public $output = [
        'list' => [
            [
                'id' => 'label=id;',
                'house_id' => 'label=房间id;',
                'building_id' => 'label=楼栋id;',
                'building_name' => 'label=楼栋号;',
                'house_name' => 'label=房间号;',
                'state' => 'label=状态： 0退租 1在租 2过期 ;',
                'start_time' => 'label=租赁开始时间;',
                'end_time' => 'label=租赁结束时间;',
            ]
        ]
    ];

    public function run($param, $uid)
    {
        $model = new Model();
        $sql = "SELECT a.id, a.house_id, a.building_id, c.`name` AS building_name, d.`name` AS house_name,
a.start_time, a.end_time, a.state
FROM `k_house_contract` as a
LEFT JOIN k_building AS c ON c.id=a.building_id
LEFT JOIN k_house AS d ON d.id=a.house_id WHERE  a.uid= {$uid};";
        $list = $model->query($sql);
        foreach($list as &$item){
            if($item['end_time']<date('Y-m-d H:i:s')){
                $item['state'] = 2;
            }
        }
        return [
            'list' => $list,
        ];
    }
}
